/**
 * Created by cuppi on 2017/7/17.
 */

import GlobalConstant from './GlobalConstant'

let instance = null;
class NativeBridge{

  constructor (){
    if (!instance) {
      instance = this;
      global.userFaceCallback = this.userFaceCallback.bind(this);
      this.userFacePromiseCallback = null;
    }
    return instance;
  }

  static defaultManager(){
    return new NativeBridge();
  }

  userFaceCallback(base64){
    if (this.userFacePromiseCallback){
      if (base64){
        this.userFacePromiseCallback.resolve(base64);
      } else {
        this.userFacePromiseCallback.reject();
      }
      this.userFacePromiseCallback = null;
    }
  }

  getUserFace(){
    return new Promise((resolve, reject) => {
      this.userFacePromiseCallback = {resolve, reject};
      if (GlobalConstant.useStaticUserFaceBase64){
        this.userFaceCallback(GlobalConstant.userFaceBase64);
      } else {
        if (global.getUserFace){
          global.getUserFace();
        }
      }
    });
  }
}

export default NativeBridge;
